﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TPDF;


namespace TPDFViewerDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;

        }

        public List<WriteableBitmap> writeableBitmaps = new List<WriteableBitmap>();
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //dd1.InvalidateVisual();
            //var _pdfComponent = new PDFComponent();
            //_pdfComponent.ZoomComponent.CurrentZoomPercentage = 100;
            //dd1.PDFPageComponent = _pdfComponent.PageComponent;
            //dd1.PDFZoomComponent = _pdfComponent.ZoomComponent;
            //_pdfComponent.OpenDocument(@"C:\Users\Alex\Desktop\1.pdf", () =>
            //{
            //    // ToDo: Hard coded text.


            //    return null;
            //});
            //dd1.InvalidateVisual();
            TPdfDocument pdfDocument = new TPdfDocument(@"C:\Users\Alex\Desktop\9999999999999999999991607706328322.pdf");
            //for (int i = 0; i < pdfDocument.Pages.Count; i++)
            //{
            //    WriteableBitmap bitmap = new WriteableBitmap((int)pdfDocument.Pages[i].Width, (int)pdfDocument.Pages[i].Height, 72, 72, PixelFormats.Bgra32, null);
            //    pdfDocument.Pages[i].Render(bitmap);
            //    writeableBitmaps.Add(bitmap);
            //}
            //itemcontrol.ItemsSource = writeableBitmaps;
            //dd.Pages = pdfDocument.Pages;
            //Task.Run(() =>
            //{
            PdfToImages(this, pdfDocument);
            //});
            //if (pdfDocument.FirstPage != null)
            //{
            //    WriteableBitmap bitmap = new WriteableBitmap((int)pdfDocument.FirstPage.Width, (int)pdfDocument.FirstPage.Height, 72, 72, PixelFormats.Bgra32, null);
            //    pdfDocument.FirstPage.Render(bitmap);
            //    image.Source = bitmap;
            //}
        }


        private void PdfToImages(MainWindow pdfViewer, TPdfDocument pdfDoc)
        {
            var items = pdfViewer.PagesContainer.Items;
            items.Clear();

            if (pdfDoc == null) return;

            for (int i = 0; i < pdfDoc.PageCount; i++)
            {
                var page = pdfDoc.Pages[i];
                WriteableBitmap bitmap = new WriteableBitmap((int)page.Width, (int)page.Height, 72, 72, PixelFormats.Bgra32, null);
                page.Render(bitmap);

                var image = new Image
                {
                    Stretch = Stretch.Fill,
                    Source = bitmap,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    Margin = new Thickness(0, 4, 0, 4),
                    MaxWidth = 800
                };
                items.Add(image);
            }
        }
        //private static async Task<WriteableBitmap> PageToBitmapAsync(TPage page)
        //{
        //    BitmapImage image = new BitmapImage();

        //    using (var stream = new InMemoryRandomAccessStream())
        //    {
        //        await page.RenderToStreamAsync(stream);

        //        image.BeginInit();
        //        image.CacheOption = BitmapCacheOption.OnLoad;
        //        image.StreamSource = stream.AsStream();
        //        image.EndInit();
        //    }

        //    return image;
        //}
    }
}
